package com.ruoyi.web.controller.customer;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.quartz.dto.CreateWorkDto;
import com.ruoyi.quartz.task.GsTask;
import com.ruoyi.system.domain.SysAimodel;
import com.ruoyi.system.mapper.SysAimodelMapper;
import com.ruoyi.system.mapper.UserTaskMapper;
import com.ruoyi.web.dto.WorkGroupDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.UserTask;
import com.ruoyi.system.service.IUserTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * AIGS任务Controller
 *
 * @author ruoyi
 * @date 2024-11-21
 */
@RestController
@RequestMapping("/system/task")
@Api(tags = "AIGC任务控制器")
public class UserTaskController extends BaseController {
    @Autowired
    private IUserTaskService userTaskService;
    @Autowired
    private UserTaskMapper userTaskMapper;
    @Autowired
    private SysAimodelMapper aimodelMapper;

    /**
     * 查询AIGS任务列表
     */
//    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserTask userTask) {
        startPage();
        if (1 != getUserId()){
            userTask.setCreateBy(getUserId().toString());
        }
        List<UserTask> list = userTaskService.selectUserTaskList(userTask);
        return getDataTable(list);
    }

    /**
     * 导出AIGS任务列表
     */
//    @PreAuthorize("@ss.hasPermi('system:task:export')")
    @Log(title = "AIGS任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserTask userTask) {
        List<UserTask> list = userTaskService.selectUserTaskList(userTask);
        ExcelUtil<UserTask> util = new ExcelUtil<UserTask>(UserTask.class);
        util.exportExcel(response, list, "AIGS任务数据");
    }

    /**
     * 获取AIGS任务详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userTaskService.selectUserTaskById(id));
    }

    /**
     * 新增AIGS任务
     * 227711622
     */
    @Log(title = "AIGS任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserTask userTask) {
        userTask.setCreateBy(getUserId().toString());
        SysAimodel aimodel = aimodelMapper.selectSysAimodelById(Long.parseLong(userTask.getAimodel()));
        userTask.setModelImg(aimodel.getUrl());
        //任务未完成，标记为禁止状态
        userTask.setCreateBy("-1");
        return toAjax(userTaskService.insertUserTask(userTask));
    }

    /**
     * 修改AIGS任务
     */
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "AIGS任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserTask userTask) {
        return toAjax(userTaskService.updateUserTask(userTask));
    }

    /**
     * 删除AIGS任务
     */
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "AIGS任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userTaskService.deleteUserTaskByIds(ids));
    }
//    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "增加AIGC任务", businessType = BusinessType.INSERT)
    @PostMapping("addList")
    @ApiOperation("增加AIGC任务")
    public AjaxResult addList(@RequestBody WorkGroupDto dto){
        //根据提交，补齐默认的时间，创建者信息
        UserTask userTask = new UserTask();
        SysAimodel aimodel = aimodelMapper.selectSysAimodelById(Long.parseLong(dto.getUserTask().getAimodel()));
        BeanUtils.copyProperties(dto.getUserTask(), userTask);
        userTask.setModelImg(aimodel.getUrl());
        userTask.setCreateBy(getUserId().toString());
        userTask.setName("正在制作中。。。");
        userTask.setProcessProgress("0%");
        //获取任务id
        int a =0;
        for (int i = 0; i < dto.getWorkpath().size(); i++) {
            CreateWorkDto createWorkDto = GsTask.createWork(dto.getWorkpath().get(i),userTask.getSourceLang(),userTask.getLang(),userTask.getWyVoiceParam());
            if (1000 == createWorkDto.getCode()){
                if (createWorkDto.getBody().getIdProject()>0 ){
                    userTask.setIdProject(String.valueOf(createWorkDto.getBody().getIdProject()));
                }
                if (createWorkDto.getBody().getIdWorks()>0){
                    userTask.setIdWorks(String.valueOf(createWorkDto.getBody().getIdWorks()));
                }
            }else {
                //状态码3表示任务创建阶段失败，用户可以点击重试再次创建
                userTask.setTask("3");
            }
            userTaskService.insertUserTask(userTask);
            a++;
        }
        return toAjax(a);
    }


}
